<template>
    <div class="container">
        <div class="row">
            <div class="col-md-4 col-md-offset-4">
                <div class="user-login text-center">
                    <h2>系统登陆</h2>
                        <div class="form-group">
                            <input v-model="param.email" class="form-control" type="text" placeholder="邮箱"/>
                        </div>
                        <div class="form-group">
                            <input v-model="param.password" class="form-control" type="password" placeholder="密码"/>
                        </div>
                        <div class="form-group">
                            <button type="button" @click="login" class="form-control btn btn-sm btn-primary">登陆</button>
                        </div>
                        <div class="form-group  text-right">
                            <router-link to="/register">注册账号</router-link>
                        </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                param: {
                    email: '',
                    password: ''
                }
            }
        },
        methods: {
            login: function (event){
                var _this = this;
                axios.post('/api/admin/login', _this.param)
                    .then(function (res) {
                        if(res.data.code === 200){
                            _this.$cookie.set('admin.name', res.data.data.admin.name, 1);
                            _this.$cookie.set('admin.id', res.data.data.admin.id, 1);
                            _this.$cookie.set('token', res.data.data.token, 1);
                            _this.$router.push({path: '/admin/main'})
                        }else if(res.data.code === 401){
                            _this.$notify({
                                group: 'auth',
                                type: 'erroe',
                                text: res.data.msg,
                            });
                        }
                    })
                    .catch(function (error) {
                        _this.$notify({
                            group: 'auth',
                            type: 'error',
                            title: 500,
                            text: '请求服务器发生错误',
                        });
                    });
            },
        },
        mounted() {

        },
    }
</script>
